﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace BtoBSWT315.User
{
    public partial class UpLoadImg_Company : System.Web.UI.Page
    {
        private void Page_Load(object sender, System.EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
            if (Session["user"] == null)
            {
                Response.Redirect("../Default.aspx");
            }
            else
            {
                //取出现有的图片
                string sql = String.Format("select cimg from qyml where [user]='{0}'", Session["user"].ToString());
                Admin.Admin_ClassDb classdb = new Admin.Admin_ClassDb();
                DataTable dataTable = null;
                dataTable = classdb.GetTables(sql);
                if (dataTable != null && dataTable.Rows.Count > 0)
                {
                    foreach (DataRow row in dataTable.Rows)
                    {
                        Image1.ImageUrl = GetImageUrl(row["cimg"].ToString());
                        TextBox1.Text = row["cimg"].ToString();
                    }
                    dataTable.Clear();
                    dataTable.Dispose();
                }
            }
        }

        protected string GetImageUrl(string imgUrl)
        {
            imgUrl = imgUrl.Replace("..", "~/");
            imgUrl = imgUrl.Replace("\\", "/");
            imgUrl = imgUrl.Trim();
            return imgUrl;
        }

        private void Button1_Click(object sender, System.EventArgs e)
        {

            #region 先上传图片
            if (!String.IsNullOrEmpty(DefaultPicUrl.PostedFile.FileName))
            {
                if (Admin.HtmlInputFileControl.IsAllowedExtension(DefaultPicUrl))
                {
                    if (Admin.HtmlInputFileControl.IsAllowedLength(DefaultPicUrl))
                    {
                        TextBox1.Text = Admin.HtmlInputFileControl.CoverFile(DefaultPicUrl, TextBox1.Text, "Company");
                    }
                    else
                    {
                        Response.Write("<script>alert('上传图片过大，请选择较小的图片上传！');</script>");
                        Response.Write("<script>history.go(-1);</script>");
                        Response.End();
                    }
                }
                else
                {
                    Response.Write("<script>alert('只支持gif/jpg/jpeg/bmp/png');</script>");
                    Response.Write("<script>history.go(-1);</script>");
                    Response.End();
                }
            }
            else
            {
                Response.Write("<script>alert('请先选择上传的图片！');</script>");
                Response.Write("<script>history.go(-1);</script>");
                Response.End();
            }


            #endregion

            //定义所需SQl语句
            string sql = "update qyml set cimg='" + TextBox1.Text + "' where [user]='" + Session["user"] + "'";
            //引入数据库操作类
            Admin.Admin_ClassDb classdb = new Admin.Admin_ClassDb();
            if (classdb.GetNonResult(sql) != 0)
            {
                Response.Write("<script>alert('图片已成功上传，请刷新！');</script>");
            }
            else
            {
                Response.Write("<script>alert('上传错误，请重试！');</script>");
                Response.Write("<script>history.go(-1);</script>");
                Response.End();
            }

        }


        #region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.Button1.Click += new System.EventHandler(this.Button1_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

    }
}